package com.yungu.swift.admin.route.operation;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.assets.driver.model.dto.DriverExamineDto;
import com.yungu.swift.assets.driver.model.param.AdminExaminePageParam;
import com.yungu.swift.assets.driver.service.DriverExamineService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.constants.MagicConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

/**
 * @author : cuixiuyin
 * @date : 2019/7/18
 */
@RestController
@RequestMapping("/admin/v1/operation/driver/examine")
@Api(value = "【运营管理】", tags = "司机考核")
public class DriverExamineAdmin {

    @Reference
    private DriverExamineService driverExamineService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "司机考核分页查询", httpMethod = "POST")
    public ResponseData<PageVo<DriverExamineDto>> queryPage(@RequestBody AdminExaminePageParam examinePageParam) {
        return driverExamineService.queryPage(examinePageParam);
    }

    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiOperation(value = "司机考核分页信息导出", httpMethod = "POST")
    public void export(@RequestBody AdminExaminePageParam examinePageParam, HttpServletResponse response) throws Exception {
        try (OutputStream os = response.getOutputStream()) {
            List<DriverExamineDto> list = driverExamineService.exportList(examinePageParam);
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            //宽度5.38
            sheet.setColumnWidth(0, ((5 + 1) * 7 + 5) / 7 * 256);
            //宽度50.38
            sheet.setColumnWidth(1, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(2, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(3, ((20 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(4, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(5, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(6, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(7, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(8, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(9, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(10, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(11, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(12, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(13, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(14, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(15, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(16, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(17, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(18, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(19, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(20, ((15 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(21, ((25 + 1) * 7 + 5) / 7 * 256);
            sheet.setColumnWidth(22, ((25 + 1) * 7 + 5) / 7 * 256);
            //创建第一行
            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("序号");
            row.createCell(1).setCellValue("司机编号");
            row.createCell(2).setCellValue("分组");
            row.createCell(3).setCellValue("司机姓名");
            row.createCell(4).setCellValue("司机电话");
            row.createCell(5).setCellValue("平均评分");
            row.createCell(6).setCellValue("投诉");
            row.createCell(7).setCellValue("上班时长（小时）");
            row.createCell(8).setCellValue("服务时长（小时）");
            row.createCell(9).setCellValue("在线时长（小时）");
            row.createCell(10).setCellValue("空驶时长（小时）");
            row.createCell(11).setCellValue("离线时长（小时）");
            row.createCell(12).setCellValue("服务里程（公里）");
            row.createCell(13).setCellValue("空驶里程（公里）");
            row.createCell(14).setCellValue("接客里程（公里）");
            row.createCell(15).setCellValue("接单量");
            row.createCell(16).setCellValue("成单量");
            row.createCell(17).setCellValue("取消量");
            row.createCell(18).setCellValue("关闭订单量");
            row.createCell(19).setCellValue("成单金额");
            row.createCell(20).setCellValue("分享乘客注册人数");
            row.createCell(21).setCellValue("分享司机注册人数");
            for (int rowNum = 1; rowNum <= list.size(); rowNum++) {
                Row dataRow = sheet.createRow(rowNum);
                dataRow.createCell(0).setCellValue(rowNum);
                DriverExamineDto bean = list.get(rowNum - 1);
                for (int cellNum = 1; cellNum <= MagicConstant.INT_TWENTY_THREE; cellNum++) {
                    Cell cell = dataRow.createCell(cellNum);
                    switch (cellNum) {
                        case 1: {
                            if (bean.getDriverUniqueId() != null) {
                                cell.setCellValue(bean.getDriverUniqueId());
                            } else {
                                cell.setCellValue("");
                            }
                            break;
                        }
                        case 2: {
                            if (bean.getDriverNo() != null) {
                                cell.setCellValue(bean.getDriverNo());
                            } else {
                                cell.setCellValue("");
                            }
                            break;
                        }
                        case 3: {
                            if (bean.getName() != null) {
                                cell.setCellValue(bean.getName());
                            }
                            break;
                        }
                        case 4: {
                            cell.setCellValue(bean.getMobile());
                            break;
                        }
                        case 5: {
                            if (bean.getScore() != null) {
                                cell.setCellValue(bean.getScore());
                            } else {
                                cell.setCellValue("");
                            }
                            break;
                        }
                        case 6: {
                            cell.setCellValue(bean.getComplainedCount());
                            break;
                        }
                        case 7: {
                            cell.setCellValue(bean.getWorkTime());
                            break;
                        }
                        case 8: {
                            cell.setCellValue(bean.getServiceTime());
                            break;
                        }
                        case 9: {
                            cell.setCellValue(bean.getOnlineTime());
                            break;
                        }
                        case 10: {
                            cell.setCellValue(bean.getEmptyTime());
                            break;
                        }
                        case 11: {
                            cell.setCellValue(bean.getOfflineTime());
                            break;
                        }
                        case 12: {
                            cell.setCellValue(bean.getServiceMileage());
                            break;
                        }
                        case 13: {
                            cell.setCellValue(bean.getEmptyMileage());
                            break;
                        }
                        case 14: {
                            cell.setCellValue(bean.getEmptyLoadMileage());
                            break;
                        }
                        case 15: {
                            cell.setCellValue(bean.getReceivedOrderCount());
                            break;
                        }
                        case 16: {
                            cell.setCellValue(bean.getCompletedOrderCount());
                            break;
                        }
                        case 17: {
                            cell.setCellValue(bean.getCanceledOrderCount());
                            break;
                        }
                        case 18: {
                            cell.setCellValue(bean.getClosedOrderCount());
                            break;
                        }
                        case 19: {
                            cell.setCellValue(bean.getTotalFare());
                            break;
                        }
                        case 20: {
                            cell.setCellValue(bean.getInvitedPassengerCount());
                            break;
                        }
                        case 21: {
                            cell.setCellValue(bean.getInvitedDriverCount());
                            break;
                        }
                        default:
                            break;
                    }
                }
            }
            response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode("driverExamine.xlsx", "UTF-8"));
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            workbook.write(os);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
